#include "status_code.h"
#include "error_status.h"
#include "allocation.h"
#include <stdio.h>


enum STATUS_ERROR htx_build_status_code (char *buf, enum HTTP_STATUS status, enum HTTP_VERSION http_v)
{
	const char *status_str = NULL;
	const char *http_version = NULL;

	switch (http_v) {
		case HTTP_1_0:
			http_version = "HTTP/1.0";
			break;
		case HTTP_1_1:
			http_version = "HTTP/1.1";
			break;
		case HTTP_2_0:
			http_version = "HTTP/2.0";
			break;
	}

	switch (status) {
		case STATUS_CONTINUE:
			status_str = "Continue";
			break;
		case STATUS_SWITCHING_PROTOCOLS:
			status_str = "Switching Protocols";
			break;
		case STATUS_OK:
			status_str = "OK";
			break;
		case STATUS_CREATED:
			status_str = "Created";
			break;
		case STATUS_ACCEPTED:
			status_str = "Accepted";
			break;
		case STATUS_NON_AUTHORITATIVE_INFORMATION:
			status_str = "Non-Authoritative Information";
			break;
		case STATUS_NO_CONTENT:
			status_str = "No Content";
			break;
		case STATUS_RESET_CONTENT:
			status_str = "Reset Content";
			break;
		case STATUS_PARTIAL_CONTENT:
			status_str = "Partial Content";
			break;
		case STATUS_MULTIPLE_CHOICES:
			status_str = "Multiple Choices";
			break;
		case STATUS_MOVED_PERMANENTLY:
			status_str = "Moved Permanently";
			break;
		case STATUS_MOVED_TEMPORARILY:
			status_str = "Moved Temporarily";
			break;
		case STATUS_SEE_OTHER:
			status_str = "See Other";
			break;
		case STATUS_NOT_MODIFIED:
			status_str = "Not Modified";
			break;
		case STATUS_USE_PROXY:
			status_str = "Use Proxy";
			break;
		case STATUS_BAD_REQUEST:
			status_str = "Bad Request";
			break;
		case STATUS_UNAUTHORIZED:
			status_str = "Unauthorized";
			break;
		case STATUS_PAYMENT_REQUIRED:
			status_str = "Payment Required";
			break;
		case STATUS_FORBIDDEN:
			status_str = "Forbidden";
			break;
		case STATUS_NOT_FOUND:
			status_str = "Not Found";
			break;
		case STATUS_METHOD_NOT_ALLOWED:
			status_str = "Method Not Allowed";
			break;
		case STATUS_NOT_ACCEPTABLE:
			status_str = "Not Acceptable";
			break;
		case STATUS_PROXY_AUTHENTICATION_REQUIRED:
			status_str = "Proxy Authentication Required";
			break;
		case STATUS_REQUEST_TIMEOUT:
			status_str = "Request Time-Out";
			break;
		case STATUS_CONFLICT:
			status_str = "Conflict";
			break;
		case STATUS_GONE:
			status_str = "Gone";
			break;
		case STATUS_LENGTH_REQUIRED:
			status_str = "Length Required";
			break;
		case STATUS_PRECONDITION_FAILED:
			status_str = "Precondition Failed";
			break;
		case STATUS_REQUEST_ENTITY_TOO_LARGE:
			status_str = "Request Entity Too Large";
			break;
		case STATUS_REQUEST_URI_TOO_LARGE:
			status_str = "Request-URI Too Large";
			break;
		case STATUS_UNSUPPORTED_MEDIA_TYPE:
			status_str = "Unsopported Media Type";
			break;
		case STATUS_INTERNAL_SERVER_ERROR:
			status_str = "Internal Server Error";
			break;
		case STATUS_NOT_IMPLEMENTED:
			status_str = "Not Implemented";
			break;
		case STATUS_BAD_GATEWAY:
			status_str = "Bad Gateway";
			break;
		case STATUS_SERVICE_UNAVAILABLE:
			status_str = "Service Unavailable";
			break;
		case STATUS_GATEWAY_TIMEOUT:
			status_str = "Gateway Time-Out";
			break;
		case STATUS_HTTP_VERSION_NOT_SUPPORTED:
			status_str = "HTTP Version not supported";
			break;
	}

	if (!buf || !status_str || !http_version) {
		return ERR_BAD_ARGUMENT;
	}

	snprintf (buf, ALLOCATED_BUF_STATUS_CODE_SIZE, "%s %s\r\n", status_str, http_version);
}
